package com.sims.util;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

public class HttpSessionUtil {

	public static void login(Integer userId, String userType, Integer userClassId) {
		HttpSession session = ServletActionContext.getRequest().getSession();
		session.setAttribute("userId", userId);
		session.setAttribute("userType", userType);
		session.setAttribute("userClassId", userClassId);
		session.setMaxInactiveInterval(30*60);
		return;
	}
	
	public static void logout() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		session.removeAttribute("userId");
		session.removeAttribute("userType");
		session.removeAttribute("userClassId");
		return;
	}
	
	public static boolean isLogin() {
		return (getUserId() != null);
	}
	
	public static Integer getUserId() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		return (Integer)session.getAttribute("userId");
	}
	
	public static String getUserType() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		return (String)session.getAttribute("userType");
	}
	
	public static Integer getUserClassId() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		return (Integer)session.getAttribute("userClassId");
	}
	
	public static String getSessionId() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		return session.getId();
	}
	
	/** Judge Whether incoming userId of the request is the same as 
	 *  the id in the session
	 * @param userId
	 * @return true/false
	 */
	public static boolean isSameUser(Integer userId) {
		return getUserId().equals(userId);
	}
}
